package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Business;
import com.wn.class30.entity.MyorderShare;
import com.wn.class30.entity.MyorderShareCount;
import com.wn.class30.entity.MyorderShareExample;
import com.wn.class30.mapper.MyorderDao;
import com.wn.class30.mapper.MyorderShareDao;
import com.wn.class30.service.BackMyorderShareService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
@Slf4j
@RestController
public class BackMyorderShareController {
    @Resource
    private BackMyorderShareService backMyorderShareService;
    @Resource
    private MyorderShareDao myorderShareDao;
    @RequestMapping("/houtai/page/finance/myordershare/list")
    @RequiresPermissions("/houtai/page/finance/myordershare/list")
    public Result getList()throws Exception{
        return new Result("200","success",null,null);
    }
    @RequestMapping("/houtai/finance/getBusiMallFinances")
    public Result getMyorderShareByPage(Integer page,Integer limit,String businessName,String businessTel) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf(page));
        map.put("limit",String.valueOf(limit) );
        map.put("businessName", businessName);
        map.put("businessTel", businessTel);
        Page pager = new Page();
        pager.setCurrentPage(page);
        pager.setPageSize(limit);
        int count = backMyorderShareService.selectCount((HashMap) map);
        pager.setTotalSize(count);
        List<Business> businesses = (List) backMyorderShareService.select((HashMap) map);
        List<MyorderShareCount> list = new ArrayList<>();
        for (Business business : businesses) {
            MyorderShareCount myorderShareCount = new MyorderShareCount();
            myorderShareCount.setBusinessId(business.getBusinessId());
            myorderShareCount.setBusinessName(business.getBusinessName());
            myorderShareCount.setBusinessTel(business.getBusinessTel());
            MyorderShareExample ex = new MyorderShareExample();
            MyorderShareExample.Criteria criteria = ex.createCriteria();
            criteria.andBusinessIdEqualTo(business.getBusinessId());
            List<MyorderShare> myorderShares = myorderShareDao.selectByExample(ex);
            double totalMoney = 0;
            double unShareMoney = 0;
            for (MyorderShare myorderShare : myorderShares) {
                totalMoney += myorderShare.getMyorderTotalPrice();
            }
            myorderShareCount.setTotalMoney(totalMoney);
            criteria.andMyorderShareStatusEqualTo(0);
            List<MyorderShare> myorderShares2 = myorderShareDao.selectByExample(ex);
            for (MyorderShare myorderShare : myorderShares2) {
                unShareMoney+= myorderShare.getMyorderTotalPrice();
            }
            myorderShareCount.setUnShareMoney(unShareMoney);
            list.add(myorderShareCount);
        }
        return new Result("0","success",pager, list);
    }
    //修改某商家所有未结算订单
    @RequestMapping("/houtai/page/finance/updateAllByBusiId")
    @RequiresPermissions("/houtai/page/finance/myordershare/update")
    public Result updateMyorderShareCount(Integer businessId)throws Exception{
        MyorderShareExample ex = new MyorderShareExample();
        MyorderShareExample.Criteria criteria = ex.createCriteria();
        criteria.andBusinessIdEqualTo(businessId);
        List<MyorderShare> li = myorderShareDao.selectByExample(ex);
        for (MyorderShare myorderShare : li) {
            if(myorderShare.getMyorderShareStatus()==0){
                myorderShare.setMyorderShareStatus(1);
                myorderShare.setMyorderShareTime(new Date());
                myorderShareDao.updateByPrimaryKeySelective(myorderShare);
            }

        }

        return new Result("0","success",null, null);
    }
    //byshareId修改结算状态
    @RequestMapping("/houtai/page/finance/updateByMyorderShareId")
    @RequiresPermissions("/houtai/page/finance/update")
    public Result updateByMyorderShareId(Integer myorderShareId)throws Exception{
        log.info("-----in"+myorderShareId);
        MyorderShareExample ex = new MyorderShareExample();
        MyorderShareExample.Criteria criteria = ex.createCriteria();
        criteria.andMyorderShareIdEqualTo(myorderShareId);
        List<MyorderShare> li = myorderShareDao.selectByExample(ex);

        for (MyorderShare myorderShare : li) {
            log.info("-----in2");
            if(myorderShare.getMyorderShareStatus()==0){
                log.info("-----in3");
                myorderShare.setMyorderShareStatus(1);
                myorderShare.setMyorderShareTime(new Date());
                myorderShareDao.updateByPrimaryKeySelective(myorderShare);
            }
        }

        return new Result("0","success",null, null);
    }

}
